﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Diagnostics;
using System.IO.Ports;
using WFNetLib.PacketProc;
using WFNetLib;
using Common;
using WFNetLib.StringFunc;
using System.Threading;
using System.Media;

namespace 生命体征测试仪
{
    public partial class formMedical : formBase
    {
        public formMedical()
        {
            InitializeComponent();
            bChange = false;
        }
        bool bChange;
        public const int ExitTime = 180;
        public MedicalResultData MedicalResult;
        private void formMedical_Load(object sender, EventArgs e)
        {
            leftTime = ExitTime;
            lbName.Text = formMain.curUser.Name;
            if (!SysConfig.bHaveShenGao)
                medicalTab.TabPages.RemoveByKey("tabShenGaoTiZhong");
            if (!SysConfig.bHaveTiWen)
                medicalTab.TabPages.RemoveByKey("tabErWen");
            if (!SysConfig.bHaveXueYa)
                medicalTab.TabPages.RemoveByKey("tabXueYa");
            if (!SysConfig.bHaveXinDian)
                medicalTab.TabPages.RemoveByKey("tabXinDian");
            if (!SysConfig.bHaveXueYang)
            {
                medicalTab.TabPages.RemoveByKey("tabXueYang");
                medicalTab.TabPages.RemoveByKey("tabNiaoSuan");
                medicalTab.TabPages.RemoveByKey("tabDanGuChun");
            }
            if (!SysConfig.bHaveXueZhi)
                medicalTab.TabPages.RemoveByKey("tabXueZhi");
            if (!SysConfig.bHaveXueTang)
                medicalTab.TabPages.RemoveByKey("tabXueTang");
            if (!SysConfig.bHaveZhiFangYi)
                medicalTab.TabPages.RemoveByKey("tabZhiFang");
            if (medicalTab.TabPages.Count == 0)
            {
                this.Close();
                return;
            }
            MedicalResult=new MedicalResultData();
            MedicalResult.ID = formMain.curUser.ID;
            MedicalResult.TEID = StringsFunction.byteToHexStr(SoftVerify_byMachineCode.GetMachineCode(), "");
            if (formMain.PType == 1)
                MedicalResult.TemporaryUserID = formMain.curUser.ID;
            medicalTab.SelectTab(0);
            XueTangResult.Text = "";
            DanGuChunResult.Text = "";
            XueNiaoSuanResult.Text = "";
            lXueTang.Text = "";
            ErWenResult.Text = "";

            lXPResult.Text = "";
            lShenGao.Text = "---.-";

            lTiZhong.Text = "---.-";
            lTZ.Text = "";
            ErWenResult.Text = "--.-";

            lGaoYa.Text = "---";
            lDiYa.Text = "---";
            lXinLv.Text = "---";

            lXueTang.Text = "";
            XueTangResult.Text = "";

            lNiaoSuan.Text = "";
            XueNiaoSuanResult.Text = "";

            lDanGuChun.Text = "";
            DanGuChunResult.Text = "";

            ZhiFang_Result.Text = "";

            lYaoWei.Text = "---.-";
            lTunWei.Text = "---.-";
        }

        private void skinButton1_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void medicalTab_Selecting(object sender, TabControlCancelEventArgs e)
        {
            leftTime = ExitTime;
//             switch (e.Action)
//             {
//                 case TabControlAction.Selecting:
//                     switch (e.TabPage.Name)
//                     {
//                         case "tabShenGaoTiZhong":
//                             break;
//                         case "tabErWen":
//                             if (!InitErWen())
//                                 e.Cancel = true;
//                             break;
//                         case "tabXinDian":
//                             if (!InitXinDian())
//                                 e.Cancel = true;
//                             break;
//                     }
//                     break;
//             }
        }

        private void serialPort1_DataReceived(object sender, System.IO.Ports.SerialDataReceivedEventArgs e)
        {

        }

        private void formMedical_FormClosing(object sender, FormClosingEventArgs e)
        {
//             XinDian_ComClosing = true;
//             while (XinDian_ComListening)
//                 Application.DoEvents();                
//             XinDianCom.Close();
        }

        private void medicalTab_SelectedIndexChanged(object sender, EventArgs e)
        {
            leftTime = ExitTime;
        }
        
        private void tabShenGaoTiZhong_Enter(object sender, EventArgs e)
        {
            leftTime = ExitTime;
        }

        private void tabShenGaoTiZhong_Leave(object sender, EventArgs e)
        {
            leftTime = ExitTime;
        }
        private void btShenGaoRead_Click(object sender, EventArgs e)
        {
            leftTime = ExitTime;
            int retry = 2;
            SoundPlayer sp;
            while(retry!=0)
            {
                lXPResult.Text = "请稍等";
                sp = new SoundPlayer();
                sp.SoundLocation = System.Windows.Forms.Application.StartupPath + "\\语音\\" + "身高1.wav";
                sp.PlaySync();                
                if (SysConfig.XPCom == null)
                {
                    lXPResult.Text = "与传感器连线失败，请检查连线";
                    return;
                }
                SysConfig.XPCom.ReadTimeout = 5000;//5s超时
                CP1616Packet cp = CP1616Packet.CP1616ComProc(ref SysConfig.XPCom, 0x01, 0x0001, null, 1);
                SysConfig.XPCom.ReadTimeout = 500;//5s超时
                if (cp == null)
                {
                    lXPResult.Text = "与传感器连线失败，请检查连线";
                    return;
                }
                float f = (float)(BytesOP.MakeShort(cp.Data[0], cp.Data[1]));
                f = f / 10;
                f = 192.7f - f;
                if (f < 70 || f > 220)
                {
                    sp = new SoundPlayer();
                    sp.SoundLocation = System.Windows.Forms.Application.StartupPath + "\\语音\\" + "身高2.wav";
                    sp.PlaySync();
                    lXPResult.Text = "数据错误，重新测量";
                    retry--;
                    continue;
                }
                MedicalResult.ShenGao = f;
                bChange = true;
                lShenGao.Text = f.ToString("F1");
                lXPResult.Text = "测量完成";
                sp = new SoundPlayer();
                sp.SoundLocation = System.Windows.Forms.Application.StartupPath + "\\语音\\" + "测量完成.wav";
                sp.PlaySync();
                break;
            }
            if (retry == 0)
            {
                lXPResult.Text = "数据错误，请检查后重新开始测量";
                sp = new SoundPlayer();
                sp.SoundLocation = System.Windows.Forms.Application.StartupPath + "\\语音\\" + "数据错误.wav";
                sp.PlaySync();
            }
        }
        private void btTiZhongRead_Click(object sender, EventArgs e)
        {
            leftTime = ExitTime;
            int retry = 2;
            SoundPlayer sp;
            while (retry != 0)
            {
                lTZ.Text = "请稍等";
                sp = new SoundPlayer();
                sp.SoundLocation = System.Windows.Forms.Application.StartupPath + "\\语音\\" + "体重1.wav";
                sp.PlaySync();                
                if (SysConfig.XPCom == null)
                {
                    lTZ.Text = "与传感器连线失败，请检查连线";
                    return;
                }
                SysConfig.XPCom.ReadTimeout = 5000;//5s超时
                CP1616Packet cp = CP1616Packet.CP1616ComProc(ref SysConfig.XPCom, 0x02, 0x0001, null, 1);
                SysConfig.XPCom.ReadTimeout = 500;//5s超时
                if (cp == null)
                {
                    lTZ.Text = "与传感器连线失败，请检查连线";
                    return;
                }
                float f = (float)(BytesOP.MakeInt(cp.Data[0], cp.Data[1], cp.Data[2], cp.Data[3]));
                f = f / 1000;
                if (f < 30 || f > 250)
                {
                    sp = new SoundPlayer();
                    sp.SoundLocation = System.Windows.Forms.Application.StartupPath + "\\语音\\" + "体重2.wav";
                    sp.PlaySync();
                    lXPResult.Text = "数据错误，重新测量";
                    retry--;
                    continue;
                }
                MedicalResult.TiZhong = f;
                bChange = true;
                lTiZhong.Text = f.ToString("F2");
                lTZ.Text = "测量完成";
                sp = new SoundPlayer();
                sp.SoundLocation = System.Windows.Forms.Application.StartupPath + "\\语音\\" + "测量完成.wav";
                sp.PlaySync();
                break;
            }
            if (retry == 0)
            {
                lTZ.Text = "数据错误，请检查后重新开始测量";
                sp = new SoundPlayer();
                sp.SoundLocation = System.Windows.Forms.Application.StartupPath + "\\语音\\" + "数据错误.wav";
                sp.PlaySync();
            }
        }
        WaitingProc waitingProc;
        bool bSend;
        private void skinButton2_Click(object sender, EventArgs e)
        {
            ExitTimer.Enabled = false;
            leftTime = ExitTime;
#if(Web)
            if(bChange)
            {
                waitingProc = new WaitingProc();
                bSend = false;
                WaitingProcFunc wpf = new WaitingProcFunc(WaitingProcFunc);
                waitingProc.Execute(wpf, "准备数据中，请等待", WaitingType.None, "");
                if (!bSend)
                {
                    return;
                }
            }
            bChange = false;
#endif
            formReport f = new formReport(MedicalResult);
            f.ShowDialog();
            this.Close();
        }
        void WaitingProcFunc(object LockWatingThread)
        {
            waitingProc.SetProcessBar(50);
            MedicalResult.fujianqingdan = SysConfig.fjqd;
            RegResult ret = WebWork.SaveMedicalResult(MedicalResult);
            if (ret == null || ret.message != "ok")
            {
                MessageBox.Show("向服务器发送数据失败，请稍后重试");                
            }
            else
            {
                waitingProc.SetProcessBar(100);
                bSend = true;
            }
        }
        private void tabTiZhong_Click(object sender, EventArgs e)
        {
            leftTime = ExitTime;
        }

        private void tabTiZhong_Enter(object sender, EventArgs e)
        {
            leftTime = ExitTime;
        }

        private void tabTiZhong_Leave(object sender, EventArgs e)
        {
            leftTime = ExitTime;
        }

        private void btMyShenGao_Click(object sender, EventArgs e)
        {
            leftTime = ExitTime;
            formInputPass f = new formInputPass("请输入身高,厘米为单位",".","数据范围为70cm至220cm",70,220);
            f.ShowDialog();
            if (f.bOK)
            {
                double d;
                if (double.TryParse(f.strInput, out d))
                {
                    MedicalResult.ShenGao = d;
                    bChange = true;
                    lShenGao.Text = d.ToString("F1");
                }
            }
        }

        private void btMyTiZhong_Click(object sender, EventArgs e)
        {
            leftTime = ExitTime;
            formInputPass f = new formInputPass("请输入体重,公斤为单位", ".", "数据范围为30kg至250kg", 30, 250);
            f.ShowDialog();
            if (f.bOK)
            {
                double d;
                if (double.TryParse(f.strInput, out d))
                {
                    MedicalResult.TiZhong = d;
                    bChange = true;
                    lTiZhong.Text = d.ToString("F2");
                }
            }
        }

        private void btMyYaoWei_Click(object sender, EventArgs e)
        {
            leftTime = ExitTime;
            formInputPass f = new formInputPass("请输入腰围,厘米为单位", ".","数据范围为20cm至150cm",20,150);
            f.ShowDialog();
            if (f.bOK)
            {
                double d;
                if (double.TryParse(f.strInput, out d))
                {
                    MedicalResult.YaoWei = d;
                    bChange = true;
                    lYaoWei.Text = d.ToString("F1");
                }
            }
        }

        private void btMyTunWei_Click(object sender, EventArgs e)
        {
            leftTime = ExitTime;
            formInputPass f = new formInputPass("请输入臀围,厘米为单位", ".", "数据范围为20cm至150cm", 20, 150);
            f.ShowDialog();
            if (f.bOK)
            {
                double d;
                if (double.TryParse(f.strInput, out d))
                {
                    MedicalResult.TunWei = d;
                    bChange = true;
                    lTunWei.Text = d.ToString("F1");
                }
            }
        }
        int leftTime;
        private void ExitTimer_Tick(object sender, EventArgs e)
        {
            leftTime--;
            if (leftTime == 0)
                this.Close();
            else
                lExitTime.Text = leftTime.ToString();
        }                                         
    }
}
